package com.dc.project.sales.controller;

import com.dc.common.core.MasterSlaveDto;
import com.dc.common.validated.InsertGroup;
import com.dc.common.validated.QueryPageGroup;
import com.dc.common.validated.SubmitGroup;
import com.dc.common.validated.UpdateGroup;
import com.dc.common.vo.ResultVo;
import com.dc.project.sales.dto.SalOrderDto;
import com.dc.project.sales.entity.SalOrder;
import com.dc.project.sales.entity.SalOrderDetail;
import com.dc.project.sales.service.SalOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 销售订单表(SalOrder)表控制层
 *
 * @author zhuangcy
 * @since 2021-06-12 16:37:10
 */
@RestController
@RequestMapping("salOrder")
@Api(tags = "销售订单表")
public class SalOrderController {
    /**
     * 服务对象
     */
    @Autowired
    private SalOrderService salOrderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    @RequiresPermissions("sales:salOrder:query")
    public ResultVo get(@PathVariable Serializable id) {
        return this.salOrderService.queryById(id);
    }

    /**
     * 分页查询销售订单表
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询销售订单表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("sales:salOrder:query")
    public ResultVo listPage(@Validated(QueryPageGroup.class) SalOrderDto dto) {
        return this.salOrderService.queryByPage(dto);
    }

    /**
     * 新增销售订单表
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增销售订单表")
    @RequiresPermissions("sales:salOrder:add")
    public ResultVo add(@Validated(value = {InsertGroup.class}) @RequestBody MasterSlaveDto<SalOrder, SalOrderDetail> dto) {
        return this.salOrderService.add(dto);
    }

    /**
     * 编辑销售订单表
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("编辑销售订单表")
    @RequiresPermissions("sales:salOrder:edit")
    public ResultVo edit(@Validated(value = {UpdateGroup.class}) @RequestBody MasterSlaveDto<SalOrder, SalOrderDetail> dto) {
        return this.salOrderService.edit(dto);
    }

    /**
     * 根据主键删除销售订单表
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除销售订单表")
    @RequiresPermissions("sales:salOrder:delete")
    public ResultVo delete(@PathVariable Serializable id) {
        return this.salOrderService.delete(id);
    }

    /**
     * 销售订单批量提交
     *
     * @param dto
     * @return
     */
    @PutMapping("submit")
    @ApiOperation("销售订单批量提交")
    @RequiresPermissions("sales:salOrder:submit")
    public ResultVo submit(@Validated(SubmitGroup.class) @RequestBody MasterSlaveDto<SalOrder, SalOrderDetail> dto) {
        return this.salOrderService.submit(dto.getIds());
    }

    /**
     * 销售订单批量撤回
     *
     * @param dto
     * @return
     */
    @PutMapping("revoke")
    @ApiOperation("销售订单批量撤回")
    @RequiresPermissions("sales:salOrder:revoke")
    public ResultVo revoke(@Validated(SubmitGroup.class) @RequestBody MasterSlaveDto<SalOrder, SalOrderDetail> dto) {
        return this.salOrderService.revoke(dto.getIds());
    }

    /**
     * 通过单据号查询单条数据
     *
     * @param docCode
     * @return
     */
    @GetMapping("one")
    @ApiOperation("通过单据号查询单条数据")
    @RequiresPermissions("sales:salOrder:query")
    public ResultVo getOneByDocCOde(@NotNull(message = "单据号不能为空") @RequestParam("docCode") String docCode) {
        return this.salOrderService.getOneByDocCOde(docCode);
    }

}